//
//  LSOpinionTableViewController.m
//  ZLFastDoctor
//
//  Created by 李宗帅 on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "LSOpinionTableViewController.h"
#import <Masonry.h>
#import "LSOpinionView.h"
#import "UIView+XYFrame.h"
#import "LSWelcomeSecionView.h"
#import <SVProgressHUD.h>
#define LSOpinionTableViewIdentifier @"LSOpinionTableViewIdentifier"
@interface LSOpinionTableViewController ()<LSOpinionViewDelegate,UITextViewDelegate,UIWebViewDelegate>

@property (nonatomic,strong) LSOpinionView *opinionView;

@end

@implementation LSOpinionTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.scrollEnabled = NO;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:LSOpinionTableViewIdentifier];
    
    
    //设置组头高
    self.tableView.sectionHeaderHeight = 44;
    
    self.tableView.backgroundColor = NQColor(242, 242, 242);
    
    
    self.navigationItem.title = @"意见反馈";
    
    [self setupOpinionView];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
}


- (void)setupOpinionView {
    
    self.tableView.tableHeaderView = self.opinionView;
    
}

//点击递交按钮的代理方法
- (void)clickSentBtnWithContentText:(NSString *)contentText {
    
//    NSLog(@"输入的建议是%@",contentText);
    if (contentText.length) {
        
        [SVProgressHUD showSuccessWithStatus:@"感谢您的反馈,我们会再接再厉"];
        
    } else {
        
        [SVProgressHUD showErrorWithStatus:@"抱歉,您还没有输入"];
    }
    
}

- (LSOpinionView *)opinionView {
    if (_opinionView == nil) {
        _opinionView = [LSOpinionView toHeardView];
        
        _opinionView.delegate = self;
        
        _opinionView.frame = CGRectMake(0, 0, 0, NQScreenH/2);
    }
    
    return _opinionView;
}

//实现代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSOpinionTableViewIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LSOpinionTableViewIdentifier];
    }
    
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.imageView.size = CGSizeMake(30, 30);
    
    
    if (indexPath.row == 0) {
        
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        
        cell.textLabel.text = @" 客服电话:400-633-1113";
    } else if (indexPath.row == 1) {
        
        cell.imageView.image = [UIImage imageNamed:@"icon_qq"];
        
        cell.textLabel.text = @"用户交流群:464768569";
    }
    
    return cell;
}

//返回每一组的头部标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    LSWelcomeSecionView *welconmeTell = [[LSWelcomeSecionView alloc]init];
    
    return welconmeTell;
}


//实现cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        [self clickCellTELWithTitle:@"是否拨打客服电话" andMessage:@"400-633-1113" TEL:@"13533647297"];
    } else if (indexPath.row == 1) {
        [self clickCellTELWithTitle:@"是否与QQ:249973495对话" andMessage:@"该操作很尴尬" QQ:@"249973495"];
    }
}

//拨打中间显示的提示框
- (void)clickCellTELWithTitle:(NSString*)title andMessage:(NSString*)message TEL:(NSString *)tel {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    
    UIAlertAction *Make = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self makeTEL:tel];
    }];
    
    [alert addAction:cancel];
    
    [alert addAction:Make];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)clickCellTELWithTitle:(NSString*)title andMessage:(NSString*)message QQ:(NSString *)qq {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    
    UIAlertAction *Make = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self makeQQ:qq];
    }];
    
    [alert addAction:cancel];
    
    [alert addAction:Make];
    
    [self presentViewController:alert animated:YES completion:nil];
}

//拨打电话的方法
- (void)makeTEL:(NSString *)tell {
    
    UIWebView *webView = [[UIWebView alloc]init];
    
    NSString *urlString = [NSString stringWithFormat:@"tel://%@",tell];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
    
}

- (void)makeQQ:(NSString *)string {
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    
    NSString *urlString = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",string];
    
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    webView.delegate = self;
    
    [webView loadRequest:request];
    [self.view addSubview:webView];
}

@end
